package cn.changeforyou.web.controller;

import cn.changeforyou.web.dao.UserDao;
import cn.changeforyou.web.entity.TestCombineFieldCondition;
import cn.changeforyou.web.entity.TestConditionTypeCondition;
import cn.changeforyou.web.entity.User;
import cn.changeforyou.web.entity.UserVO;
import cn.changeforyou.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author zhyu
 * @version 1.0
 * @date 2022/2/8 16:22
 */
@RestController
@RequestMapping("test")
public class UserController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserService userService;

    @GetMapping("getById")
    public Object getById(Integer id) {
        return userDao.selectById(id);
    }

    @GetMapping("getVOById")
    public UserVO getVOById(Integer id) {
        return userDao.selectVOById(id, UserVO.class);
    }

    @GetMapping("getVOsByEntity")
    public List<UserVO> getVOsById(User user) {
        return userDao.selectVOListByEntity(user, UserVO.class);
    }

    @GetMapping("listAll")
    public List<User> listAll() {
        return userService.getAllEntities();
    }

    @GetMapping("selectMaps")
    public List<Map<String, Object>> selectMaps() {
        return userService.getAllMap();
    }

    @GetMapping("selectMapsByCondition")
    public Object selectMapsByCondition(User user) {
        return userDao.selectListByCondition(user);
    }

    @GetMapping("selectByEntity")
    public User selectByEntity(User user) {
        return userDao.selectByEntity(user);
    }

    @GetMapping("selectByCondition")
    public User selectByCondition(TestConditionTypeCondition condition) {
        return userDao.selectByCondition(condition);
    }

    @GetMapping("pageByCondition")
    public Page<User> pageByCondition(TestConditionTypeCondition condition, @PageableDefault(value = 10, page =
            1, sort = {"create_time"}, direction = Sort.Direction.DESC) Pageable pageable) {
        return userService.pageByCondition(condition, pageable);
    }

    @GetMapping("selectListByCondition_typeCondition")
    public List<User> selectListByCondition_typeCondition(TestConditionTypeCondition condition) {
        return userDao.selectListByCondition(condition);
    }

    @GetMapping("selectListByCondition_combineFieldCondition")
    public List<User> selectListByCondition_combineFieldCondition(TestCombineFieldCondition condition) {
        return userDao.selectListByCondition(condition);
    }

    @GetMapping("countByEntity")
    public int countByEntity(User user) {
        return userDao.countByEntity(user);
    }

}
